import 'dart:convert';
import 'package:freedom/page/mahjong/bloc/room_info.dart';
import 'package:freedom/page/mahjong/bloc/user_state.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/common/message_content.dart';
import 'package:freedom/page/mahjong/event/user_state_event.dart';
import 'package:freedom/page/mahjong/message/abstract_action_message.dart';

class NextGameingAction extends AbstractMessageActionHandler<MahjongMessage> {
  @override
  void doMessage(MahjongMessage message) {
    RoomInfo roomInfo = RoomInfo.formJSON(jsonDecode(message.data!));
    MahjongContext().setRoomInfo(roomInfo);
    /** 更新所有用户的状态 */
    roomInfo.gameInfo.allUser?.forEach((item) {
      MahjongContext.setUserState(
          MahjongContext.getUserDirection(item.userId), item);
      MahjongContext.postUserEvent<UserStateUpdateEvent>(
          item.userId, UserStateUpdateEvent(item));
    });
    UserState? currentUserState = roomInfo.gameInfo.allUser?.singleWhere(
        (item) => item.userId == MahjongContext.currentUser?.userId);
    /** 显示战绩 */
    MahjongContext().showGameBalanceWidget(roomInfo);
    /** 将换牌信息调整 */
    MahjongContext().updateSwapCard(null, null);
  }
}
